package com.rocksdata.mc.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ctz.commonframe.ui.view.EmbeddedListView;
import com.ctz.commonframe.utils.DensityUtil;
import com.rocksdata.mc.entity.CourseInfo;
import com.rocksdata.mc.ui.dialog.CourseDetailDialog;
import com.rocksdata.mc.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 陈堂正 on 2017/2/13.
 */

public class DayCourseView extends LinearLayout {
    private final static int HEIGHT_PER_INDEX = 60;
    private Context context;
    private TextView tvZone;
    private EmbeddedListView lvZoneCourse;
    private List<CourseInfo> courseList = new ArrayList<CourseInfo>();
    private ZoneCourseListAdapter adapter;
    private long dateMillis;

    public DayCourseView(Context context) {
        super(context);
        init(context);
    }

    public DayCourseView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DayCourseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.view_zone_course, this);
        tvZone = (TextView) view.findViewById(R.id.tv_zone);
        lvZoneCourse = (EmbeddedListView) view.findViewById(R.id.lv_zone_course);

    }

    public void setZone(int zone){
//        switch (zone) {
//            case CourseInfo.DAY_ZONE_MORNING:
//                tvZone.setText("上午");
//                tvZone.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_morining_zone, 0, 0);
//                tvZone.setCompoundDrawablePadding(4);
//                break;
//            case CourseInfo.DAY_ZONE_AFTERNOON:
//                tvZone.setText("下午");
//                tvZone.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_afternoon_zone, 0, 0);
//                tvZone.setCompoundDrawablePadding(4);
//                break;
//            case CourseInfo.DAY_ZONE_EVENING:
//                tvZone.setText("晚上");
//                tvZone.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_evening_zone, 0, 0);
//                tvZone.setCompoundDrawablePadding(4);
//                break;
//        }
//
//        adapter = new ZoneCourseListAdapter(context, courseList, zone);
//        lvZoneCourse.setAdapter(adapter);
//        lvZoneCourse.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                CourseInfo info = adapter.getItem(position);
//                if(info != null){
//                    showCourseDetailDialog(info);
//                }
//            }
//        });
    }

    public void setData(long dateMillis, List<CourseInfo> list){
        this.dateMillis = dateMillis;
        courseList.clear();
        if(list != null)
            courseList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    private class ZoneCourseListAdapter extends BaseAdapter{
        private List<CourseInfo> datas;
        private Context context;
        private int dayZone;

        public ZoneCourseListAdapter(Context context, List<CourseInfo> datas, int dayZone) {
            this.context = context;
            this.datas = datas;
            this.dayZone = dayZone;
        }

        @Override
        public int getCount() {
//            int count = dayZone == CourseInfo.DAY_ZONE_EVENING ? 2 : 4;
//            if(datas != null && datas.size() > 0){
//                int morningCourseCount = 0;
//                int morningCourseDuration = 0;
//                int afternoonCourseCount = 0;
//                int afternoonCourseDuration = 0;
//                int eveningCourseCount = 0;
//                int eveningCourseDuration = 0;
//
//                for(CourseInfo info: datas){
//                    if(info.getLesson() < 5){
//                        morningCourseCount++;
//                        morningCourseDuration += info.duration;
//                    }else if(info.getLesson() > 8){
//                        eveningCourseCount++;
//                        eveningCourseDuration += info.duration;
//                    }else{
//                        afternoonCourseCount++;
//                        afternoonCourseDuration += info.duration;
//                    }
//                }
//                switch (dayZone){
//                    case CourseInfo.DAY_ZONE_MORNING:
//                        if(morningCourseDuration < 4){
//                            count = 4 - morningCourseDuration + morningCourseCount;
//                        }else{
//                            count = morningCourseCount;
//                        }
//                        break;
//                    case CourseInfo.DAY_ZONE_AFTERNOON:
//                        if(afternoonCourseDuration < 4){
//                            count = 4 - afternoonCourseDuration + afternoonCourseCount;
//                        }else{
//                            count = afternoonCourseCount;
//                        }
//                        break;
//                    case CourseInfo.DAY_ZONE_EVENING:
//                        if(eveningCourseDuration < 2){
//                            count = 2 - eveningCourseDuration + eveningCourseCount;
//                        }else{
//                            count = eveningCourseCount;
//                        }
//                        break;
//                }
//            }
//
//            return count;
            return 0;
        }

        @Override
        public CourseInfo getItem(int position) {
            CourseInfo courseInfo = null;
            for(CourseInfo info: datas){
                if(info.getLesson() == (dayZone - 1) * 4 + position + 1){
                    courseInfo = info;
                    break;
                }
            }
            return courseInfo;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_zone_course, null);
                holder.layoutParent = (LinearLayout) convertView.findViewById(R.id.layout_parent);
                holder.tvIndex = (TextView) convertView.findViewById(R.id.tv_course_index);
                holder.tvTeacher = (TextView) convertView.findViewById(R.id.tv_teacher_name);
                holder.tvCourseName = (TextView) convertView.findViewById(R.id.tv_course_name);
                holder.tvChangedCourseName = (TextView) convertView.findViewById(R.id.tv_changed_course_name);
                holder.tvTeacherChanged = (TextView) convertView.findViewById(R.id.tv_teacher_changed);
                holder.sameFlag = convertView.findViewById(R.id.flag_continuous_course);

                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
//            CourseInfo data = getItem(position);
//            AbsListView.LayoutParams params;
//            if(data == null){
//                holder.layoutParent.setVisibility(View.INVISIBLE);
//                params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
//                        DensityUtil.dip2px(context, HEIGHT_PER_INDEX));
//            }else{
//                holder.layoutParent.setVisibility(View.VISIBLE);
//                params = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
//                        DensityUtil.dip2px(context, HEIGHT_PER_INDEX) * data.duration);
//                String index;
//                if(data.duration == 1){
//                    index = data.getLesson() + "";
//                    holder.sameFlag.setVisibility(View.INVISIBLE);
//                }else{
//                    index = data.getLesson() + "-" + (data.getLesson() + data.duration - 1);
//                    holder.sameFlag.setVisibility(View.VISIBLE);
//                }
//                holder.tvIndex.setText("第" + index + "节");
//
//                if(data.getChange() == CourseInfo.STATUS_NORMAL) {
//                    holder.tvChangedCourseName.setVisibility(View.GONE);
//                    holder.tvTeacherChanged.setVisibility(View.GONE);
//                    holder.tvTeacher.setText("任课老师：" + data.getStaffName());
//                    holder.tvCourseName.setText(data.getCourseName());
//                    holder.tvCourseName.setTextColor(context.getResources().getColor(R.color.color_B9B9B9));
//                }else if(data.getChange() == CourseInfo.STATUS_CHANGE_COURSE){
//                    holder.tvChangedCourseName.setVisibility(View.VISIBLE);
//                    holder.tvTeacherChanged.setVisibility(View.GONE);
//                    holder.tvTeacher.setText("任课老师：" + data.getStaffName());
//                    holder.tvChangedCourseName.setText(data.getCourseName());
//                    holder.tvCourseName.setTextColor(context.getResources().getColor(R.color.color_B9B9B9));
//                    holder.tvCourseName.setText(data.getCourseName());
//                }else if(data.getChange() == CourseInfo.STATUS_CHANGE_TEACHER){
//                    holder.tvChangedCourseName.setVisibility(View.GONE);
//                    holder.tvTeacherChanged.setVisibility(View.VISIBLE);
//                    holder.tvTeacher.setText("任课老师：" + data.getStaffName());
//                    holder.tvCourseName.setText(data.getCourseName());
//                    holder.tvCourseName.setTextColor(context.getResources().getColor(R.color.color_B9B9B9));
//                }else if(data.getChange() == CourseInfo.STATUS_TEST){
//                    holder.tvCourseName.setText("考试");
//                    holder.tvTeacherChanged.setVisibility(View.GONE);
//                    holder.tvChangedCourseName.setVisibility(View.GONE);
//                    holder.tvCourseName.setTextColor(context.getResources().getColor(android.R.color.holo_orange_dark));
//                    holder.tvTeacher.setText("任课老师：" + data.getStaffName());
//                }
//            }
//
//            convertView.setLayoutParams(params);
            return convertView;
        }

        class ViewHolder{
            LinearLayout layoutParent;
            TextView tvIndex;
            TextView tvTeacher;
            TextView tvCourseName;
            TextView tvChangedCourseName;
            TextView tvTeacherChanged;
            View sameFlag;
        }
    }

    private void showCourseDetailDialog(final CourseInfo info){
        new CourseDetailDialog(context, info, dateMillis).show();
    }
}
